﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 产生冲击波

public class Impact : MonoBehaviour
{
    public float radius;
    public float strength;
    public float coolDown;
    public GameObject impactText;

    private float timer;

    // Start is called before the first frame update
    void Start()
    {
        timer = 0.0f;
    }

    // Update is called once per frame
    void Update()
    {
        if (timer <= 0.0f)
        {
            impactText.SetActive(true);
            if (Input.GetKey(KeyCode.Space))
            {
                Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

                for (int idx = 0; idx < colliders.Length; idx++)
                {
                    if (colliders[idx].tag == "BarrierBall")
                    {
                        Rigidbody rigidbody = colliders[idx].gameObject.GetComponent<Rigidbody>();
                        Vector3 difference = colliders[idx].gameObject.transform.position - transform.position;
                        Vector3 direction = new Vector3(difference.x, difference.magnitude, difference.z).normalized;
                        rigidbody.AddForce(direction * strength);
                    }
                }

                timer = coolDown;
                impactText.SetActive(false);
            }
        }
        else
        {
            timer -= Time.deltaTime;
        }
    }
}
